package com.tyrone.gateway.transport.config;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;

import java.util.concurrent.CompletableFuture;

/**
 * @author yelong.xie
 * @date 2024/5/8 23:20
 */
public class DispatchClientChannelInitializer extends ChannelInitializer<SocketChannel> {

    private final CompletableFuture<FullHttpResponse> future;

    public DispatchClientChannelInitializer(CompletableFuture<FullHttpResponse> future) {
        this.future = future;
    }


    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();


        p.addLast(new HttpClientCodec());

//
//        // Remove the following line if you don't want automatic content decompression.
        p.addLast(new HttpContentDecompressor());

        // Uncomment the following line if you don't want to handle HttpContents.
        p.addLast(new HttpObjectAggregator(1048576));

        p.addLast(new DispatchClientCallResponseHandler(future));

    }


}
